package com.project.ssmfo.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.project.ssmfo.model.ActivityEvaluate;
import com.project.ssmfo.service.ActivityEvaluateService;
import com.project.utils.PaginationInfo;
@Controller
@RequestMapping("/activityEvaluate")
public class ActivityEvaluateController extends BaseController{
	
	static final Logger log = LoggerFactory.getLogger(ActivityEvaluateController.class);
	/**
	 * @Fields SPLITPAGE_SIZE : 活动评价管理分页，每页的数目
	 */
	public static final int SPLITPAGE_SIZE = 10;
	
	@Autowired
	ActivityEvaluateService activityEvaluateService;
	
	
	/**
	 * <p>Title: list</p>
	 * <p>Description: 活动评价管理列表</p>
	 * @param reqPage 请求页，页码
	 * @param keyword 查询关键字，模糊查询用户名
	 * @return String
	 */
	@RequestMapping("/list")
	public String list(Model model, Integer reqPage, String keyword,String activityId){
		log.info("[list] show activityEvaluate manage list ... ");
		try {
			//获取请求参数
			if (reqPage == null) {
				reqPage = 1;
			}

			Map<String, Object> params = new HashMap<String, Object>();
			if (!StringUtils.isEmpty(keyword)) {
				params.put("keyword", keyword);
			}
			if (!StringUtils.isEmpty(activityId)) {
				params.put("activityId", activityId);
			}
			
			PaginationInfo<ActivityEvaluate> pageinfo = activityEvaluateService.getPaginationData(params, reqPage, SPLITPAGE_SIZE);
			model.addAttribute("pageinfo", pageinfo);
			model.addAttribute("keyword", keyword);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "activityEvaluate/list";
	}
	
	/**
	 * <p>Title: addOrUpdate</p>
	 * <p>Description: 添加或修改活动评价信息页面</p>
	 * @param id 活动评价ID,新增活动评价时，该参数为空，非空时表示修改
	 * @return String
	 */
	@RequestMapping("addOrUpdate")
	public String addOrUpdate(Model model, Integer id){
		log.info("[addOrUpdate] add or update activityEvaluate form prepare controller .");
		if(id != null){//addUser
			ActivityEvaluate activityEvaluate = activityEvaluateService.selectByPrimaryKey(id);
			model.addAttribute("activityEvaluate", activityEvaluate);
			return "activityEvaluate/edit";
		}else{
			return "activityEvaluate/add";
		}
		
	}
	
	/**
	 * <p>Title: save</p>
	 * <p>Description: 保存活动评价信息，插入或更新</p>
	 * @param user 活动评价信息
	 * @return String
	 */
	@RequestMapping("save")
	public String save(Model model, RedirectAttributes redirectAttributes, ActivityEvaluate activityEvaluate){
		log.info("[save] save activityEvaluate info (insert or update)");
		if(activityEvaluate == null){
			log.error("[save] save activityEvaluate info error, user is null...");
		}
		try {
			if(activityEvaluate.getId() == null){//Insert data into database.
				activityEvaluate.setCreateTime(new Date());
				activityEvaluate.setDeleted(false);
				activityEvaluateService.insertSelective(activityEvaluate);
			}else{//update activityEvaluate info
				activityEvaluateService.updateByPrimaryKeySelective(activityEvaluate);
			}
			log.info("[save] save activityEvaluate info success...");
		} catch (Exception e) {
			log.error("[save] save activityEvaluate info error...");
			e.printStackTrace();
		}
		
		return "redirect:/activityEvaluate/list";
	}
	
	/**
	 * <p>Title: delete</p>
	 * <p>Description: 根据ID删除活动评价</p>
	 * @param redirectAttributes
	 * @param id
	 * @return String
	 */
	@RequestMapping("delete")
	public String delete(RedirectAttributes redirectAttributes, Integer id){
		if(id == null){
			log.error("[delete] delete activityEvaluate error, id is null");
		}else{
			try {
				ActivityEvaluate activityEvaluate = new ActivityEvaluate();
				activityEvaluate.setId(id);
				activityEvaluate.setDeleted(true);
			this.activityEvaluateService.updateByPrimaryKeySelective(activityEvaluate);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "redirect:/activityEvaluate/list";
	}
	
	
}
